<%@ page import="com.wl.shixun2.entity.Product" %>
<%@ page import="java.util.List" %>
<%@ page import="com.wl.shixun2.dao.ProductDAO" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("UTF-8");
%>
<html>
<head>
    <title>2350309097 万朗</title>
    <link rel="stylesheet" href="css/productList.css" type="text/css">
    <script>
        function confirmDelete(id, name) {
            if (confirm("确定要删除产品 \"" + name + "\" 吗？")) {
                window.location.href = "deleteProduct.jsp?id=" + id;
            }
        }
        
        // 页面加载完成后，如果存在消息，则3秒后淡出
        document.addEventListener('DOMContentLoaded', function() {
            var messageElement = document.querySelector('.message');
            if (messageElement) {
                setTimeout(function() {
                    messageElement.classList.add('fade-out');
                }, 2000); // 2秒后开始淡出
            }
        });
    </script>
</head>
<body>


<div style="text-align: center;">
    <a href="addProduct.jsp" class="add-button">新增产品</a>
    <a href="index.jsp" class="query-button">查询产品</a>
</div>

<%
    List<Product> li = null;
    try {
        li = ProductDAO.findAll();
    } catch (Exception e) {
        out.println("<div class='error'>数据加载失败: " + e.getMessage() + "</div>");
    }
%>

<table width="700" border="1" align="center" cellpadding="2" cellspacing="0">
    <tr>
        <td align="center" bgcolor="#99FFFF">学号</td>
        <td align="center" bgcolor="#99FFFF">产品号</td>
        <td align="center" bgcolor="#99FFFF">名称</td>
        <td align="center" bgcolor="#99FFFF">生产日期</td>
        <td align="center" bgcolor="#99FFFF">价格</td>
        <td align="center" bgcolor="#99FFFF">操作</td>
    </tr>
    <% 
    if(li != null) {
        for(Product pro:li) {
    %>
    <tr>
        <td align="center"><%=pro.getId() %></td>
        <td align="center"><%=pro.getNumber() %></td>
        <td align="center"><%=pro.getName() %></td>
        <td align="center"><%=pro.getMadetime() != null ? pro.getMadetime().toString() : "" %></td>
        <td align="center"><%=pro.getPrice() %></td>
        <td align="center">
            <a class="edit-link" href="editProduct.jsp?id=<%=pro.getId()%>">编辑</a>
            &nbsp;|&nbsp;
            <a class="delete-link" href="javascript:void(0)" onclick="confirmDelete(<%=pro.getId()%>, '<%=pro.getName() != null ? pro.getName().replace("'", "\\'").replace("\"", "\\\"") : ""%>')">删除</a>
        </td>
    </tr>
    <% 
        } 
    } else {
    %>
    <tr>
        <td colspan="6" align="center">暂无数据</td>
    </tr>
    <%
    }
    %>
</table>
<%
    String message = request.getParameter("message");
%>

<% if (message != null && !message.trim().isEmpty()) { %>
<div class="message"><%= message %></div>
<% } %>
</body>
</html>